#include <algorithm>
#include <cmath>
#include <cstdio>
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define mod 1000000000

const int N = 40000;
const double eps = 1e-8;

struct Node {
	int id, l, r;
} tr[N << 2];
double k[100010], b[100010];
int n, cnt, lastans;

inline double f(int id, int x) {
	return k[id] * x + b[id];
}

void build(int cur, int l, int r) {
	tr[cur].l = l, tr[cur].r = r;
	if (l == r) return;
	int mid = (l + r) >> 1;
	build(ls(cur), l, mid);
	build(rs(cur), mid + 1, r);
}

inline bool check(int a, int b, int x) {
	double fa = f(a, x), fb = f(b, x);
	return fabs(fa - fb) < eps ? a < b : fa > fb;
}

void insert(int cur, int l, int r, int id) {
	int mid = (tr[cur].l + tr[cur].r) >> 1;
	if (l <= tr[cur].l && tr[cur].r <= r) {
		if (check(id, tr[cur].id, tr[cur].l) && check(id, tr[cur].id, tr[cur].r)) {
			tr[cur].id = id;
			return;
		}
		if (!check(id, tr[cur].id, tr[cur].l) && !check(id, tr[cur].id, tr[cur].r)) return;
		if (check(id, tr[cur].id, mid)) std::swap(id, tr[cur].id);
		if (check(id, tr[cur].id, tr[cur].l))
			insert(ls(cur), l, r, id);
		else
			insert(rs(cur), l, r, id);
		return;
	}
	if (l <= mid) insert(ls(cur), l, r, id);
	if (r > mid) insert(rs(cur), l, r, id);
}

int query(int cur, int pos) {
	if (tr[cur].l == tr[cur].r) return tr[cur].id;
	int mid = (tr[cur].l + tr[cur].r) >> 1;
	int tmp = pos <= mid ? query(ls(cur), pos) : query(rs(cur), pos);
	if (check(tr[cur].id, tmp, pos)) tmp = tr[cur].id;
	return tmp;
}

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
	freopen("testdata.in", "r", stdin);
	freopen("testdata.out", "w", stdout);
#endif
#ifndef LOCAL
	freopen("LiChaoSegTree.in", "r", stdin);
	freopen("LiChaoSegTree.out", "w", stdout);
#endif
#endif

	scanf("%d", &n);
	int op, x_0, y_0, x_1, y_1, q;
	build(1, 1, 40000);
	while (n--) {
		scanf("%d", &op);
		if (op) {
			++cnt;
			scanf("%d%d%d%d", &x_0, &y_0, &x_1, &y_1);
			x_0 = (x_0 + lastans - 1) % 39989 + 1;
			x_1 = (x_1 + lastans - 1) % 39989 + 1;
			y_0 = (y_0 + lastans - 1) % mod + 1;
			y_1 = (y_1 + lastans - 1) % mod + 1;
			if (x_0 > x_1) std::swap(x_0, x_1), std::swap(y_0, y_1);
			if (x_0 == x_1)
				k[cnt] = 0, b[cnt] = std::max(y_1, y_0);
			else {
				k[cnt] = (double)(y_1 - y_0) / (x_1 - x_0);
				b[cnt] = y_1 - k[cnt] * x_1;
			}
			insert(1, x_0, x_1, cnt);
		} else {
			scanf("%d", &q);
			q = (q + lastans - 1) % 39989 + 1;
			lastans = query(1, q);
			printf("%d\n", lastans);
		}
	}
	return 0;
}